﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.Configuration;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class _01_Connection : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnSQLServer_Click(object sender, EventArgs e)
        {
            //方法二：获得数据库连接对象
            string strConn = ConfigurationManager.ConnectionStrings["SQLConn"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            conn.Open();
            lblConnStr.Text = "数据库连接字符串:" + strConn;
            lblConnState.Text = "数据库连接对象的状态:" + conn.State.ToString();
            conn.Close();
            conn.Dispose();
        }

        /// <summary>
        /// 方法一：获得数据库连接对象
        /// </summary>
        private void GetSqlConn()
        {
            string strConn = ConfigurationManager.ConnectionStrings["SQLConn"].ConnectionString;

            //使用using的好处就是 不需要额外的 关闭数据库连接,推荐使用using关键字
            using (SqlConnection conn = new SqlConnection(strConn))
            {
                conn.Open();
                lblConnStr.Text = strConn;
                lblConnState.Text = conn.State.ToString();
            }
        }

    }
}